<?php 
/**
 *  index.php API 入口
 *
 * @copyright			(C) 2005-2010 PHPCMS
 * @license				http://www.phpcms.cn/license/
 * @lastmodify			2010-7-26
 */
return;
define('PHPCMS_PATH', __DIR__.'/');
include PHPCMS_PATH.'phpcms/base.php';
$param = pc_base::load_sys_class('param');

/**
 * 网页抓取有数字报的日期
 */
function getDate2(){
    $cache = getcache("publicsh_date","epaper");
    if($cache){
        sort($cache);
        return $cache;
    }

    $publish_date = [];

    for($i=57; $i>=1; $i--){
        $url = 'https://www.chinaluan.com/lakblm_'.$i;
        $content = file_get_contents($url);
        
        $xmlDoc = new DOMDocument();
        libxml_use_internal_errors(true);
        $xmlDoc->loadHTML($content);
        libxml_use_internal_errors(false);
        
        $xmlDoc->preserveWhiteSpace=false; 
        
        // bzlist2
        $xpath = new DOMXPath($xmlDoc);
        $elements = $xpath->query("/html/body//div[@class='bzlist2']//li//img");
        foreach($elements as $el){
            $alt = $el->getAttribute('alt');
            $publish_date[] = date('Ymd',strtotime($alt));
        }
        
        // die;
        sleep(2);

    }

    setcache("publicsh_date",$publish_date,"epaper");
    return $publish_date;
}

// grab("2023-01-01");

// delcache("publicsh_date","epaper");

$arr = getDate2();

// foreach($arr as $val){
//     echo "$val\n";
// };
// die;


$epaper_upload_model = pc_base::load_model('epaper_upload_model');
foreach($arr as $date){
    $date2 = date('Y-m-d H:i:s', strtotime($date));
    $one = $epaper_upload_model->get_one(['date'=>$date2]);
    if($one){
        echo $date." skip\n";
        continue;
    }

    echo $date."\n";
    sleep(2);
    try{
        $id = $epaper_upload_model->grabAdd($date);
        $epaper_upload_model->analysisCfg($id);

    }catch(Throwable $e){
        echo $date." 抓取失败\n".$e->getMessage()."\n";
    }

}